package com.cci.kangdao.dao;

import com.cci.kangdao.dao.model.UserT;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2019/4/30
 */
@MapperScan
public interface RolesDao {

    @Select("select distinct roles.* from Roles_T roles" +
            "  inner join UserGroupHasRole_T groupRoles on roles.Flag = 0 and groupRoles.Flag = 0 and roles.ID = groupRoles.RoleId" +
            "  inner join CompanyUserGroupRelation_T relation on relation.Flag = 0 and groupRoles.UserGroupId = relation.UserGroupId " +
            "where roles.IsApply = 1 and relation.CompanyId = #{companyId} and relation.UserGroupType in (1,3)")
    List<Map<String,Object>> getCompanyRoles(Long companyId);

    @Select("select distinct roles.* from Roles_T roles" +
            "  inner join UserGroupHasRole_T groupRoles on roles.Flag = 0 and groupRoles.Flag = 0 and roles.ID = groupRoles.RoleId" +
            "  inner join CompanyUserGroupRelation_T relation on relation.Flag = 0 and groupRoles.UserGroupId = relation.UserGroupId" +
            "  inner join SSRepair_T repair on repair.Flag = 0 and relation.CompanyId = repair.CompanyID " +
            "where roles.IsApply = 1 and repair.ServiceStationID = #{serviceStationId} and relation.UserGroupType = 6")
    List<Map<String,Object>> getServiceStationRoles(Long serviceStationId);

    @Select("select * from Usergroup_T userGroup inner join CompanyUserGroupRelation_T relation on userGroup.Flag = 0 and relation.Flag = 0 and userGroup.ID = relation.UserGroupId\n" +
            "where relation.CompanyId = #{companyId} and relation.UserGroupType in (1,3)")
    List<Map<String,Object>> getUserGroupByCompanyId(Long companyId);

    @Select("select * from Roles_T roles inner join UserGroupHasRole_T userGroupRoles on roles.Flag = 0 and userGroupRoles.Flag = 0 and roles.ID = userGroupRoles.RoleId\n" +
            "where userGroupRoles.UserGroupId = #{userGroupId} and roles.RolesNo = 'R0002'")
    List<Long> getCustomRoleListByUserGroupId(Long userGroupId);

    @Select("select count(*) from SSRepair_T where Flag = 0 and ServiceStationID = #{serviceStationId}")
    Integer getParentCompanyCountByStationId(Long serviceStationId);

    @Select("select CompanyId from SSRepair_T where Flag = 0 and ServiceStationID = #{serviceStationId}")
    List<Long> getParentCompanyIdsByStationId(Long serviceStationId);

    @Select("select relation.UserGroupId from CompanyUserGroupRelation_T relation inner join SSRepair_T repair on relation.CompanyId = repair.CompanyID and relation.Flag = 0 and repair.Flag = 0\n" +
            "where repair.ServiceStationID = #{serviceStationId}")
    Long getServiceStationUserGroupId(Long serviceStationId,Integer userGroupType);

    @Select("select ID from Usergroup_T where GroupNo = 'G0013' and Flag = 0")
    Long getUnionStationUserGroupId();

    @Select("select relation.UserGroupId from CompanyUserGroupRelation_T relation\n" +
            "  inner join SSRepair_T repair on relation.CompanyId = repair.CompanyID and relation.Flag = 0 and repair.Flag = 0\n" +
            "where repair.ServiceStationID = #{serviceStationId} and relation.UserGroupId !=(select ID from Usergroup_T where GroupNo = 'G0013' and Flag = 0) and relation.UserGroupType = 6")
    Long getStationUserGroupId(Long serviceStationId);

    @Select("select relation.UserGroupId from CompanyUserGroupRelation_T relation\n" +
            "  inner join SSRepair_T repair on relation.CompanyId = repair.CompanyID and relation.Flag = 0 and repair.Flag = 0\n" +
            "where repair.ServiceStationID = #{serviceStationId} and relation.UserGroupId !=(select ID from Usergroup_T where GroupNo = 'G0013' and Flag = 0) and relation.UserGroupType = 6")
    List<Long> getStationUserGroupIds(Long serviceStationId);

    @Select("select ID from UserGroupHasRole_T where UserGroupId = #{userGroupId} and RoleId = #{roleId} and Flag = 0")
    Long getUserGroupHasRoleId(@Param("userGroupId") Long userGroupId, @Param("roleId")Long roleId);


    //获取康明斯服务站用户组下除了服务商站长以外的所有角色名称,和获取App普通用户组下所有角色
    @Select("select r.RolesName from UserGroupHasRole_T a \n" +
            "inner join Roles_T r on a.RoleId = r.ID and r.Flag = 0\n" +
            "where a.Flag = 0 and a.UserGroupId = (select ID from Usergroup_T where GroupName = #{groupName} and Flag = 0) and r.RolesName != '服务商站长'")
    List<String> getRoleNameListByGroupName(@Param("groupName") String groupName);

@Select("select count(1)\n" +
        "\t\tfrom UserHasRoles_T ur\n" +
        "        inner join UserGroupHasRole_T ghrt on ghrt.ID = ur.RolesID and ghrt.Flag = 0\n" +
        "        inner join Roles_T rt on rt.ID = ghrt.RoleId and rt.Flag = 0\n" +
        "        where ur.Flag = 0 and rt.RolesNo in ('R0041','R0116','R0033') and ur.UserID = #{userId}")
int getXgjRoleList(long id);
}
